/*
 * WinUtf8Encoder.h
 *
 *  Created on: 2011-3-19
 *      Author: terry
 */

#ifndef WINUTF8ENCODER_H_
#define WINUTF8ENCODER_H_

#include "BasicType.h"
#include <string>
#include "DataBuffer.h"


class WinUtf8Encoder
{
public:
	WinUtf8Encoder()
    {
    }

    ~WinUtf8Encoder()
    {
    }

    const char* gbkToUtf8(const char* text)
    {
        return gbkToUtf8(text, strlen(text));
    }

    const char* gbkToUtf8(const char* text, size_t length)
    {
        int unicodeLen = MultiByteToWideChar(CP_ACP, 0, text, (int)length, NULL, 0);
        m_wcharBuffer.resize(unicodeLen, 0);

        unicodeLen = MultiByteToWideChar(CP_ACP, 0, text, (int)length, m_wcharBuffer.data(), unicodeLen);

        int utf8Len = WideCharToMultiByte(CP_UTF8, 0, m_wcharBuffer.data(), unicodeLen, NULL, 0, NULL, NULL);
        m_charBuffer.resize(utf8Len + 1, 0);
        utf8Len = WideCharToMultiByte(CP_UTF8, 0, m_wcharBuffer.data(), unicodeLen,
            m_charBuffer.data(), utf8Len, NULL, NULL);
        m_charBuffer[utf8Len] = 0;
        return m_charBuffer.data();
    }

    const char* gbkToUtf8(const std::string& text)
    {
        return gbkToUtf8(text.c_str(), text.size());
    }

    const char* utf8ToGbk(const char* text)
    {
        return utf8ToGbk(text, strlen(text));
    }

    const char* utf8ToGbk(const char* text, size_t length)
    {
        int unicodeLen = MultiByteToWideChar(CP_UTF8, 0, text, (int)length, NULL, 0);
        m_wcharBuffer.resize(unicodeLen, 0);
        unicodeLen = MultiByteToWideChar(CP_UTF8, 0, text, (int)length, m_wcharBuffer.data(), unicodeLen);

        int gbkLen = WideCharToMultiByte(CP_ACP, 0, m_wcharBuffer.data(), unicodeLen, NULL, 0, NULL, NULL);
        m_charBuffer.resize(gbkLen + 1, 0);
        gbkLen = WideCharToMultiByte(CP_ACP, 0, m_wcharBuffer.data(), unicodeLen,
            m_charBuffer.data(), gbkLen, NULL, NULL);
        m_charBuffer[gbkLen] = 0;
        return m_charBuffer.data();
    }

    const char* utf8ToGbk(const std::string& text)
    {
        return utf8ToGbk(text.c_str(), text.size());
    }

private:
    comn::DataBuffer< char >		m_charBuffer;
	comn::DataBuffer< wchar_t >	m_wcharBuffer;

};


#endif /* WINUTF8ENCODER_H_ */
